
during their first few days. He said this
orientation was to introduce
apprentices to the practices that Mr. C
insists we use, and to the level of
quality he expects from our code.
Gã nói ông C nhất quyết cho rằng
phần định hướng này rất thiết thực với
người học việc, và chất lượng mã
nguồn mà ông mong đợi ở họ.
This excited me greatly. It was an
opportunity to show them how good
a programmer I am. So I told Jerry I
couldn’t wait to start. He responded by
asking me to write a simple program
for him. He wanted me to use the
Sieve of Eratosthenes to calculate
prime numbers. He told me to have
the program, including all unit tests,
ready for review just after lunch.
Tôi háo hức kinh khủng. Ðây là cơ hội
để tôi cho họ thấy mình là một tay lập
trình giỏi cỡ nào. Thế là tôi bảo Jerry
tôi không chờ được nữa. Gã đáp lại
sự háo hức của tôi bằng cách bảo tôi
thử viết một chương trình đơn giản.
Gã muốn tôi dùng Sàng Eratosthenes
(Sieve) để tính các số nguyên tố. Gã
còn bảo tôi phải chuẩn bị xong
chương trình bao gồm trọn bộ các
kiểm thử đơn vị để soát sau buổi ăn
trưa.
This was great! I had almost four
hours to whip together a simple
program like the Sieve. I am
determined to do a really impressive
job. Listing 1 shows what I wrote. I
made sure it was well commented,
and neatly formatted.
Thật tuyệt! Tôi có gần 4 giờ đồng hồ
để “xào nấu” một chương trình đơn
giản như Sieve. Tôi quyết tâm làm một
chương trình thật ấn tượng. Tôi đã
viết mã ở Mã dẫn 1. Tôi nắm chắc là
chương trình được chú thích cẩn
thận và trình bày gọn gàng.
Mã dẫn 1
/**
* This class generates prime numbers up to a user-specified maximum.
* The algorithm used is the Sieve of Eratosthenes. <p> Eratosthenes of
Cyrene, b.c.
* 276 BC, Cyrene, Libya; d.c.194 BC,Alexandria. He was the first man to
* calculate the circumference of the Earth, and was also known for working on
* calendars with leap years and running the library at Alexandria.</p> *
The
* algorithm is quite simpl
e: Given an array of integers starting at 2, cross
* out all multiples of 2. Find the next uncrossed integer, and cross out all of
* its multiples. * Repeat until you have passed the square root of the maximum
* value.
* @authorAlphonse,
Jerry said that this was better, but still
wasn’t pleased with it because it led to
double negatives like unCrossed[i] =
false. So he changed the name of the
array to isCrossed and changed the
sense of all the booleans. Then he ran
all the tests.
Jerry got rid of the initialization that
set isCrossed[0] and isCrossed[1] to
true. He said it was good enough to
just make sure that no part of the
function used the isCrossed array for
indexes less than 2. The tests all still
ran.
Jerry ex
tracted the inner loop of the
crossOutMultiples function and called
it crossOutMultiplesOf. He said that
statements like if (isCrossed[i] ==
false) were confusing so he created a
function called notCrossed and
changed the if statement to if
(notCrossed(i)). Then he ran the tests.
Then Jerry asked me what that square
root was all about. I spent a bit of time
writing a comment that tried to explain
why you only have to iterate up to the
square root of the array size.
I t
ried to emulate Jerry by extracting
the calculation into a function where I
could put the explanatory comment.
In writing the comment I realized that
the square root is the maximum prime
factor of any of the integers in the
array. So I chose that name for the
variables and functions that dealt with
it.
Finally, I made sure that the tests all
Jerry nói mã này được hơn nhưng gã
vẫn chưa hài lòng vì nó dẫn đến khả
năng phủ định kép như unCrossed[i]
= false. Bởi thế gã đổi tên mảng thành
isCros
sed và đổi ý nghĩa của mọi giá
trị luận lý. Sau đó gã chạy mọi kiểm
thử.
Jerry xóa mã gán true cho
isCrossed[0] và isCrossed[1]. Gã nói
điều này đủ tốt để đảm bảo không có
phần nào của hàm dùng mảng
isCrossed với chỉ số nhỏ hơn 2. Mọi
kiểm thử vẫn chạy.
Jerry tách phần lặp bên trong của
hàm crossOutMultiples ra và đặt tên là
crossOutMultipleOf. Gã bảo rằng các
lệnh tương tự như if (isCrossed[i] ==
false) dễ gây nhầm lẫn, nên gã tạo
hàm có tên notCrossed và thay cụm if
thành if (notCrossed(i))
. Kế tiếp gã
chạy lại mấy kiểm thử.
Sau đó Jerry hỏi tôi ý nghĩa của căn
bậc hai tôi đã dùng. Tôi tốn ít thời giờ
viết chú thích cho lý do cần phải lặp
lại cho đến căn bậc hai của độ dài
mảng.
Tôi cố làm theo Jerry bằng cách tách
phần tính toán thành một hàm, tôi có
thể viết chú thích trong hàm này.
Trong khi viết chú thích tôi nhận ra
rằng căn bậc hai là thừa số nguyên tố
lớn nhất của bất kỳ số nào trong
mảng. Sau đó tôi chọn tên cho các
hàm và biến xử lý vấn đề này
Cuối cùng tôi bảo đảm các kiểm thử
while(!(success = try()));
www.codegym.vn / Page 13